package com.imu.purse.modules.trade.service.impl;

import com.github.pagehelper.PageHelper;
import com.imu.core.base.BaseService;
import com.imu.datasources.DataSourceNames;
import com.imu.datasources.annotation.DataSource;
import com.imu.enums.DetailLogType;
import com.imu.purse.modules.bitcoin.form.YtfcoinForm;
import com.imu.purse.modules.trade.dao.TransDetailLogMapper;
import com.imu.purse.modules.trade.dao.TransLogMapper;
import com.imu.purse.modules.trade.entity.TransDetailLogEntity;
import com.imu.purse.modules.trade.entity.TransLogEntity;
import com.imu.purse.modules.trade.service.TransDetailLogService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * IMU转账上链详情表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-07-01 16:21:08
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class TransDetailLogServiceImpl extends BaseService<TransDetailLogEntity> implements TransDetailLogService 
{
	@Override
    @Transactional
    public int save(final TransDetailLogEntity transDetailLog)
	{
		transDetailLog.setCreateTime(new Date());
    	return super.insert(transDetailLog);
	}
	
	@Override
	@Transactional
    public int update(final TransDetailLogEntity transDetailLog) 
    {
    	transDetailLog.setUpdateTime(new Date());
    	return mapper.updateByPrimaryKeySelective(transDetailLog);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final TransDetailLogEntity transDetailLog)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(transDetailLog)))
    	{
        	len = this.insert(transDetailLog);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public TransDetailLogEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public List<TransDetailLogEntity> queryList(final TransDetailLogEntity transDetailLog)
    {
    	if (transDetailLog.getPage() != null && transDetailLog.getLimit() != null)
    	{
            PageHelper.startPage(transDetailLog.getPage(), transDetailLog.getLimit());
        }
    	
    	return mapper.findByPage(transDetailLog);
    }

	/**
	 * A1--->A 从A1转代币到A
	 * @param form
	 * @return
	 */
    @Transactional
    public int moYtfLog(final YtfcoinForm form){
		//根据交易ID获取信息
		TransDetailLogEntity entity = new TransDetailLogEntity();
		entity.setTransId(form.getTransId());
		entity.setTransTime(form.getTransTime());
		entity = mapper.findByTId(entity);
		if(entity != null){
			TransLogEntity tEntity = new TransLogEntity();
			tEntity.setId(entity.getLogId());
			tEntity.setUpdateTime(new Date());
			tEntity.setOrderState(DetailLogType.TOKEN_FIN.getType());
			transLogMapper.updateByPrimaryKeySelective(tEntity);

			TransDetailLogEntity tmpEntity = new TransDetailLogEntity();
			tmpEntity.setId(entity.getId());
			tmpEntity.setFee(tmpEntity.getMoney().subtract(form.getMoney()));
			tmpEntity.setUpdateTime(tEntity.getUpdateTime());
			mapper.updateByPrimaryKeySelective(tmpEntity);
		}
		return 1;
	}
	@Autowired
	private TransDetailLogMapper mapper;

    @Autowired
    private TransLogMapper transLogMapper;
}
